package com.chuangxin.data.chinabaike;

import com.chuangxin.data.core.WebPageFetcher;
import com.chuangxin.data.core.io.FileWriter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 * Created by Dawnwords on 2015/2/3.
 */
public abstract class ChinaBaiKeFetcher extends WebPageFetcher {

    private static final String BASE = "http://www.chinabaike.com";

    public ChinaBaiKeFetcher(String outDir) {
        super(0.5, new FileWriter("E:" + File.separator + "chinabaike" + File.separator + outDir, true));
    }

    @Override
    public void execute() {
        super.execute();
        new URLGenerator().start();
    }

    @Override
    protected TitleContent processDoc(Document doc, final String url) {
        String title = doc.title();
        Elements content = doc.select("div#NewsContentLabel");
        if (content.size() > 0) {
            Elements pages = doc.select("div#NewsContentLabel + p a");
            if (pages.size() > 0) {
                Elements a = pages.eq(pages.size() - 1);
                if ("下一页".equals(a.html())) {
                    final String nextURL = getBaseURL(url) + a.attr("href");
                    new Thread() {
                        @Override
                        public void run() {
                            putURL(nextURL);
                        }
                    }.start();
                }
            }
            return new TitleContent(title, content.html());
        }
        return null;
    }

    private class URLGenerator extends Thread {
        @Override
        public void run() {
            try {
                Document doc = Jsoup.connect(getDirURL()).get();
                Elements options = doc.select("select > option");
                for (int i = 0; i < options.size(); i++) {
                    addDirPage(getBaseURL(getDirURL()) + options.eq(i).attr("value"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void addDirPage(String url) {
            try {
                Document doc = Jsoup.connect(url).get();
                Elements as = doc.select("table.tablist a");
                for (int i = 0; i < as.size(); i++) {
                    putURL(BASE + as.eq(i).attr("href"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected abstract String getDirURL();

    private static String getBaseURL(String url) {
        int lastSlash = url.lastIndexOf("/");
        return url.substring(0, lastSlash + 1);
    }

}
